﻿using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Web.Model;
using System.Linq;

namespace Web.DataAccess
{
	public class UpcomingProjectsQuery
	{
        private User CurrentUser { get; set; }
        public UpcomingProjectsQuery(User currentUser)
        {
            CurrentUser = currentUser;
        }

		public IList<Project> Load(ISession session)
		{
            IList<Project> projects = session
				.CreateCriteria<Project>()
				.Add(Restrictions.Ge("Ending", DateTime.Now) || Restrictions.IsNull("Ending"))
                .AddOrder(Order.Asc("Beginning"))
				.List<Project>();
            if (CurrentUser!= null && CurrentUser.IsAdmin)
            {
                return projects;
            }
            else
            {
                return projects.Where(p => p.IsVisibleToUser(CurrentUser)).ToList();
            }
		}
	}
}